package com.contact.util.util.search;

import java.util.Collection;
import java.util.List;

import com.contact.util.Identifiable;
import com.contact.util.util.collections.Utils;
import com.trg.search.ISearch;
import com.trg.search.SearchResult;
import com.trg.search.Sort;

/**
 * This method wraps the items for list operations in controllers. The view for these pages will require this in the
 * model map.
 */
public class RequestResults<T extends Identifiable> {

    private Collection<T> items;
    private int index = 0;
    private int resultSize = 0;
    private int pageSize = 0;
    private ResultItemSort itemSort;

    public RequestResults() {
        this(null);
    }

    public RequestResults(ISearch search, SearchResult<T> searchResult) {
        if (searchResult != null) {
            setItems(searchResult.getResult());
            setResultSize(searchResult.getTotalCount());
        }

        if (search != null) {
            setIndex(search.getFirstResult());
            setPageSize(search.getMaxResults());
            List<Sort> sorts = search.getSorts();

            if (Utils.hasValues(sorts)) {
                Sort sort = sorts.get(0); //only supports one sort column
                setItemSort(new ResultItemSort(sort.getProperty(), !sort.isDesc()));
            }
        }
    }

    public RequestResults(ISearch search, Collection<T> list, int totalCount) {
        this(search, null);
        setItems(list);
        setPageSize(search.getMaxResults());
        setResultSize(totalCount);
    }

    public RequestResults(Collection<T> results) {
        this(results, Utils.size(results));
    }

    public RequestResults(Collection<T> items, int totalCount) {
        this(items, totalCount, PaginationInfo.createPaginationInfo());
    }

    public RequestResults(Collection<T> items, PaginationInfo paginationInfo) {
        this(items, Utils.size(items), paginationInfo);
    }

    public RequestResults(Collection<T> items, int totalCount, PaginationInfo paginationInfo) {
        if (Utils.isEmpty(items)) {
            return;
        }
        setItems(items);
        setResultSize(totalCount);
        setPageSize(paginationInfo.getMaxResults());
        setIndex(paginationInfo.getFirstRow());
        setItemSort(paginationInfo.getResultItemSort());
    }

    public final Collection<T> getItems() {
        return items;
    }

    public final void setItems(Collection<T> results) {
        this.items = results;
    }

    public final int getIndex() {
        return index;
    }

    public final void setIndex(int index) {
        this.index = index;
    }

    public final int getResultSize() {
        return resultSize;
    }

    public final void setResultSize(int resultSize) {
        this.resultSize = resultSize;
    }

    public final int getPageSize() {
        return pageSize;
    }

    public final void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public final boolean isEmpty() {
        return Utils.isEmpty(items);
    }

    public final boolean getHasItems() {
        return !isEmpty();
    }

    public final void setItemSort(ResultItemSort itemSort) {
        this.itemSort = itemSort;
    }

    public final ResultItemSort getItemSort() {
        return itemSort;
    }

}
